#!/bin/bash

set -o errexit  # Exit on error
set -o nounset  # Treat unset variables as an error

EXAMPLES_DIR=$(cd -- "$(dirname "$0")" && pwd)

usage() {
  echo "Usage: $0 [command]"
  echo
  echo "Run tests or a specific command in all examples"
  echo
  echo "Commands:"
  echo "  <none>  Run all example tests (both 'npm ci' and 'npm test')"
  echo "  <cmd>   Run a specific npm command (e.g., 'audit' for npm audit)"
  echo
  echo "Examples:"
  echo "  $0"
  echo "  $0 test"
  echo "  $0 audit"
  exit 0
}

run_example() {
  local example_dir="$1"
  shift
  if [ $# -gt 0 ]; then
    echo "Ignoring extra arguments: $*"
  fi

  echo "$(tput bold)$(tput setaf 7)========================================$(tput sgr0)"
  echo "$(tput bold)$(tput setaf 7)Running example in $example_dir$(tput sgr0)"
  echo "$(tput bold)$(tput setaf 7)========================================$(tput sgr0)"

  if [ -d "$example_dir/node_modules" ]; then
    echo "$(tput bold)$(tput setaf 7)Skipping installation of dependencies$(tput sgr0)"
  else
    npm --prefix "$example_dir" ci
  fi

  echo "$(tput bold)$(tput setaf 7)Running tests$(tput sgr0)"
  npm --prefix "$example_dir" run test
}

run_cmd() {
  local example_dir="$1"
  shift
  if [ $# -eq 0 ]; then
    echo "No command provided"
    exit 1
  fi
  echo "$(tput bold)$(tput setaf 7)========================================$(tput sgr0)"
  echo "$(tput bold)$(tput setaf 7)Running '$*' in $example_dir$(tput sgr0)"
  echo "$(tput bold)$(tput setaf 7)========================================$(tput sgr0)"
  npm --prefix "$example_dir" "$@"
}

main() {
  if [ "${1-}" = "--help" ] || [ "${1-}" = "-h" ]; then
    usage
  fi

  declare -a examples=(
    "$EXAMPLES_DIR/v2/e2e"
    "$EXAMPLES_DIR/v2/graphql"
    "$EXAMPLES_DIR/v2/jest"
    "$EXAMPLES_DIR/v2/messages"
    "$EXAMPLES_DIR/v2/mocha"
    "$EXAMPLES_DIR/v2/serverless"
    "$EXAMPLES_DIR/v2/typescript"
    "$EXAMPLES_DIR/v3/e2e"
    "$EXAMPLES_DIR/v3/graphql"
    "$EXAMPLES_DIR/v3/provider-state-injected"
    "$EXAMPLES_DIR/v3/run-specific-verifications"
    "$EXAMPLES_DIR/v3/todo-consumer"
    "$EXAMPLES_DIR/v3/typescript"
    "$EXAMPLES_DIR/v4/graphql"
    "$EXAMPLES_DIR/v4/matchers"
    "$EXAMPLES_DIR/v4/messages"
    "$EXAMPLES_DIR/v4/multipart"
    "$EXAMPLES_DIR/v4/plugins"
    "$EXAMPLES_DIR/v4/typescript"
  )

  if [ $# -gt 0 ]; then
    for example in "${examples[@]}"; do
      run_cmd "$example" "$@"
    done
  else
    declare -a failed_examples=()
    for example in "${examples[@]}"; do
      run_example "$example" || failed_examples+=("$example")
    done

    if [ ${#failed_examples[@]} -ne 0 ]; then
      for failed_example in "${failed_examples[@]}"; do
        echo "$(tput bold)$(tput setaf 1)Failed to run example in $failed_example$(tput sgr0)"
      done
    fi
  fi
}

main "$@"
